package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈方凯
 * @date 2020/6/15
 * description:找工队的viewModel
 */
public class FindPackersAtvModel extends AndroidViewModel {

    private PickSelectEntity all;

    private int page = 1;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<FindPackersEntity>> teamList = new MutableLiveData<>();
    public MutableLiveData<String> h5Url = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> categoryType = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> teamSize = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> city = new MutableLiveData<>();

    public FindPackersAtvModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getNewList(String teamSize, String firstCategoryNo, String secondCategoryNo, String thirdCategoryNo, String teamType, String provinceNo, String cityNo, String areaNo, boolean b,String publishTime) {
        Map<String, Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        int limit = 10;
        map.put("limit", limit);
        map.put("page", page);
        if (!TextUtils.isEmpty(teamSize) && !TextUtils.equals("全部", teamSize)) {
            map.put("teamNumber", teamSize);
        }

        if (!"".equals(firstCategoryNo) && firstCategoryNo != null) {
            map.put("firstCategoryNo", firstCategoryNo);
        }
        if (!"".equals(secondCategoryNo) && secondCategoryNo != null) {
            map.put("secondCategoryNo", secondCategoryNo);
        }
        if (!"".equals(thirdCategoryNo) && thirdCategoryNo != null) {
            map.put("thirdCategoryNo", thirdCategoryNo);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNoList", stringList);
        }
        if (!"".equals(teamType) && teamType != null) {
            map.put("teamType", teamType);
        }

        if (!"".equals(publishTime) && publishTime != null){
            map.put("publishTime",publishTime);
        }

        int companyType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (companyType == 2){
            map.put("teamNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<FindPackersEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<FindPackersEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                teamList.postValue(data.getData().getList());
            }
        });
    }

    /**
     * 工人详情H5链接
     *
     * @param id
     */
    public void getH5Url(String id) {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                String url = data.getData().getUrl() + "?no=" + id + "&type=0";
                h5Url.postValue(url + "----" + id);
            }
        });
    }

    /**
     * 工程-工程分类
     */
    public void getCategoryType() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCategoryListTree(0).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> data) {
                        DialogHelper.dismissProgressDialog();
                        categoryType.postValue(data.getData());
                    }
                });
    }

    /**
     * 团队规模
     *
     * @param type
     */
    public void getTeamSize(int type,String areaCode) {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getTeamScale(areaCode, "" + type).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data1) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> data = data1.getData();
                data.add(0, all);
                teamSize.postValue(data);
            }
        });
    }

    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                city.postValue(data.getData());
            }
        });
    }
}
